package com.woniu51.search.rabbitmq;

import com.woniu.common.constant.UserConstants;
import com.woniu51.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 杨光
 * @date 2022年04月12日 11:45
 */
@Component
public class MessageListener {


    @Autowired
    SearchService searchService;

    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(name = UserConstants.MQ_ES_EXCHANGE,
                            type = ExchangeTypes.TOPIC),
                    value = @Queue(value = UserConstants.MQ_ES_MOVIE_QUEUE, durable = "true"),
                    key = {UserConstants.MQ_ES_MOVIE_ROUTING_KEY}
            )
    })
    public void receiveMovieMessage(Integer movieId){
        System.out.println("收到movie 更新消息："+movieId);
        searchService.refreshMovie(movieId);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(name = UserConstants.MQ_ES_EXCHANGE,
                            type = ExchangeTypes.TOPIC),
                    value = @Queue(value = UserConstants.MQ_ES_CINEMA_QUEUE, durable = "true"),
                    key = {UserConstants.MQ_ES_CINEMA_ROUTING_KEY}
            )
    })
    public void receiveCinemaMessage(Integer cinemaId){
        System.out.println("收到影院 更新消息："+cinemaId);
        searchService.refreshCinemas(cinemaId);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(name = UserConstants.MQ_ES_EXCHANGE,
                            type = ExchangeTypes.TOPIC),
                    value = @Queue(value = UserConstants.MQ_ES_ACTOR_QUEUE, durable = "true"),
                    key = {UserConstants.MQ_ES_ACTOR_ROUTING_KEY}
            )
    })
    public void receiveActorMessage(Integer actorId){
        System.out.println("收到演员 更新消息："+actorId);
        searchService.refreshActors(actorId);
    }
}
